$(function() {
    let keyWords = getQueryString("keyWords");
    keyWords=decodeURI(keyWords);
    console.log("keyWords:"+keyWords);
    getProductOnSearch(keyWords);
})

function getProductOnSearch(keyWords) {

    let url = "/product/selectProductOnSearch";
    let params={
        keyWords:keyWords
    }
    httpGet(url,params,function (data) {
        renderingSearchProduct(data);
    })

}

function renderingSearchProduct(productList) {
    let templateProductHtml = $("#template_product_search").html();
    let productListHtml = "";
    let tempProductHtml;
    //遍历商品列表
    $.each(productList,function (index, product) {
        //后台数据填充到模板
        tempProductHtml=templateProductHtml.replace(/{name}/g,product.name)
            .replace(/{info}/g,product.info)
            .replace(/{id}/g,product.productId)
            .replace(/{picture}/g,product.picture)
        productListHtml+=tempProductHtml;
    });
    console.log(productListHtml);
    $("#div_product_search").html(productListHtml);

}

function getQueryString(name) {
    //获取地址栏url的参数
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}